<?php
/**
 * Created by PhpStorm.
 * User: Ason
 * Date: 15-6-23
 * Time: 下午4:29
 *
 * 用户管理
 */
class User extends Admin_controller{
	
	public $json = [ 'status'=>'error', 'error'=>'未知错误' ];

	public function __construct(){
		parent::__construct();
		parent::logged_check();
		$this->load->model( 'user_model' );
	}

	/**
	 * 用户管理
	 * */
	public function index(){
			
		$this->data[ 'users' ] = $this->user_model->get_all_users();
		$this->data[ 'groups' ] = $this->user_model->get_all_groups();

		$this->view( 'user/user' );
	}
	/**
	 * 编辑用户信息
	 * */
	public function edit_user( $id ){
		
		if( $this->data[ 'user' ] = $this->user_model->get_user_by_id( $id ) ){
			$this->data[ 'group' ] = $this->user_model->get_all_groups();
			$this->load->view( 'user/part/edit_user', $this->data );
		}
	}
	/**
	 * 编辑用户信息ajax-post
	 * */
	public function do_edit_user( $id ){
		
		$id = (int)$id;
		$info = $this->input->post( null, TRUE );
		if( $this->user_model->edit_user( $id, $info ) ){
			$this->json[ 'status' ] = 'success';
			$this->json[ 'error' ] = '成功修改用户信息！';
		}else{
			$this->json[ 'error' ] = '数据库操作失败！请重新尝试！';
		}
		echo json_encode( $this->json ); return;
	}
	/**
	 * 删除用户
	 * */
	public function do_del_user( $id ){
		
		$id = (int)$id;
		//不能删除id=1的用户
		if( $id == 1 ){
			$this->json[ 'error' ] = '不能删除超级管理员帐户';
		}else{
			if( $this->user_model->del_user( $id ) ){
				$this->json[ 'status' ] = 'success';
				$this->json[ 'error' ] = '用户删除成功！';
			}else{
				$this->json[ 'error' ] = '数据库操作失败！请稍后重试！';
			}
		}
		echo json_encode( $this->json );return;
	}
	/**
	 * 重置用户密码
	 * */
	public function do_reset_password( $id ){
		
	}

	/**
	 * 用户组管理
	 * */
	public function group(){
		
		$this->data[ 'user_group' ] = $this->db->get( 'user_group' )->result();

		$this->view( 'user/group' );

	}
	
	/**
	 * 添加用户组ajax-post
	 * */
	 public function do_add_group(){
		 
		 $json = [ 'status'=>'error', 'error'=>'未知错误' ];
		 
		 $post = $this->input->post( null, true );

		 $this->load->model( 'user_model' );

		 if( empty( $post['name'] ) ){
			 $json[ 'error' ] = '请填写用户组名';
		 }elseif( $this->user_model->get_group_by_name( $post['name'] ) ){
			$json[ 'error' ] = '用户组名称已存在';
		 }else{
			if( $this->user_model->add_group( $post ) ){
				$json[ 'status' ] = 'success';
				$json[ 'error' ] = '用户组创建成功';
			}else{
				$json[ 'error' ] = '写入用户组数据失败';
			}
		 }
		 
		 echo json_encode( $json );
	 }
	
	/**
	 * 编辑用户组信息
	 * 
	 * */
	public function edit_group( $id ){

		$this->load->model( 'user_model' );
		$this->data[ 'group_info' ] = $this->user_model->get_group_by_id( $id );
		$this->data[ 'permit' ] = explode( ',',  $this->data[ 'group_info' ][ 'permit' ] );
		$this->load->model( 'sidebar_model' );
		$this->data[ 'all_menu' ] = $this->sidebar_model->get_all_menu();
		
		$this->load->view( 'user/part/edit_group', $this->data );
	}


	/**
	 * 修改用户组信息ajax-post
	 */
	public function do_edit_group( $id ){
		
		$json = [ 'status'=>'error', 'error'=>'未知错误' ];
		$post = $this->input->post( null, true );
		
		$this->load->model( 'user_model' );
		
		if( empty( $post['group_name'] ) ){
			$json[ 'error' ] = '请填写用户组名 ';
			echo json_encode( $json ); return FALSE;
		}
		
		$data[ 'name' ] = $post[ 'group_name' ];
		$data[ 'remark' ] = $post[ 'group_remark' ];
		$data[ 'permit' ] = empty( $post[ 'group_right' ] ) ? '' : implode( ',', $post[ 'group_right' ] );

		if( $this->user_model->edit_group( $id, $data ) ){
			$json[ 'status' ] = 'success';
			$json[ 'error' ] = '用户组修改成功！';
		}else{
			$json[ 'error' ] = '写入数据错误！';
		}
		echo json_encode( $json );  return FALSE;
	}
	
	/*
		删除用户组
	*/
	public function do_del_group( $id ){
		
	}

	/**
	 * 修改个人信息
	 * */
	public function edit_info( $id ){
		
		$this->data[ 'user' ] = $this->user_model->get_user_by_id( $id );
		$this->load->view( 'user/part/edit_info', $this->data );
	}
	
	/**
	 * 修改个人密码
	 * */
	public function change_pwd(){
		
		$this->load->view( 'user/part/change_pwd' );
	}
	/**
	 * 修改个人密码ajax-post
	 * */
	public function do_change_pwd(){
		
		$post = $this->input->post( null, TRUE );
		if( empty( $post['password'] ) || empty( $post[ 'password1' ] ) ){
			$this->json[ 'error' ] = '两次密码都不能为空！';
		}elseif( $post['password'] != $post['password1'] ){
			$this->json[ 'error' ] = '两次密码不一致！请认真核对！';
		}else{
			if( $this->user_model->edit_user( sess('id'), array( 'password'=>md5( $post['password'] ) ) ) ){
				$this->json[ 'status' ] = 'success';
				$this->json[ 'error' ] = '修改密码成功！';
			}else{
				$this->json[ 'error' ] = '数据库操作失败！请稍后重试！';
			}
		}
		echo json_encode( $this->json ); return;
	}
	
}
